<?php
namespace app\store\controller\content;

use app\store\controller\Controller;
use app\store\model\Page as PageModel;

/**
 * 单页控制器
 * Class Page
 * @package app\store\controller\content
 */
class Page extends Controller
{
    /**
     * 单页列表
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $model = new PageModel;
        $list = $model->getList();
        return $this->fetch('index', compact('list'));
    }

    /**
     * 添加单页
     * @return array|mixed
     */
    public function add()
    {
        $model = new PageModel;
        if (!$this->request->isAjax()) {
            return $this->fetch('add');
        }
        // 新增记录
        if ($model->add($this->postData('page'))) {
            return $this->renderSuccess('添加成功', url('content.page/index'));
        }
        return $this->renderError($model->getError() ?: '添加失败');
    }

    /**
     * 更新单页
     * @param $page_id
     * @return array|mixed
     * @throws \think\exception\DbException
     */
    public function edit($page_id)
    {
        // 单页详情
        $model = PageModel::detail($page_id);
        if (!$this->request->isAjax()) {
            return $this->fetch('edit', compact('model'));
        }
        // 更新记录
        if ($model->edit($this->postData('page'))) {
            return $this->renderSuccess('更新成功', url('content.page/index'));
        }
        return $this->renderError($model->getError() ?: '更新失败');
    }

    /**
     * 删除单页
     * @param $page_id
     * @return array
     * @throws \think\exception\DbException
     */
    public function delete($page_id)
    {
        // 单页详情
        $model = PageModel::detail($page_id);
        if (!$model->setDelete()) {
            return $this->renderError($model->getError() ?: '删除失败');
        }
        return $this->renderSuccess('删除成功');
    }


}